package com.caigou.modules.famous.repository

import com.caigou.comm.base.bean.HttpResult
import com.caigou.comm.base.bean.PrizeBallBean
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.famous.bean.*
import com.caigou.modules.famous.network.HttpFamousService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/09
 */
@ActivityRetainedScoped
class FamousRepository @Inject constructor() {

    fun getFamousList(dareTypeId: Int, type: Int, sort: Int, currentUserId: String): Flow<HttpResult<FamousBean>> {
        return HttpFamousService.service.getFamousList(dareTypeId, type, sort, currentUserId)
    }

    fun getContestList(dareTypeId: Int, type: Int, userId: String, currentUserId: String): Flow<HttpResult<PersonalBean>> {
        return HttpFamousService.service.getContestList(dareTypeId, type, userId, currentUserId)
    }

    fun getSubscribeRecordList(dareTypeId: Int, type: Int, userId: String): Flow<HttpResult<MutableList<FamousItemBean>>> {
        return HttpFamousService.service.getSubscribeRecordList(dareTypeId, type, userId)
    }

    fun getSubscribe2otherList(type: Int, userId: String): Flow<HttpResult<MutableList<SubscribeItemBean>>> {
        return HttpFamousService.service.getSubscribe2otherList(type, userId)
    }

    fun getSubscribe2meList(type: Int, userId: String): Flow<HttpResult<MutableList<SubscribeItemBean>>> {
        return HttpFamousService.service.getSubscribe2meList(type, userId)
    }

    fun getMyChallengeList(dareTypeId: Int, type: Int, userId: String): Flow<HttpResult<MyChallengeBean>> {
        return HttpFamousService.service.getMyChallengeList(dareTypeId, type, userId)
    }

    fun getRewardRecordList(type: Int): Flow<HttpResult<MutableList<MyChallengeItemBean>>> {
        return HttpFamousService.service.getRewardRecordList(type)
    }

    fun getChallengeParamsList(dareTypeId: Int): Flow<HttpResult<MutableList<ChallengeParamsItemBean>>> {
        return HttpFamousService.service.getChallengeParamsList(dareTypeId)
    }

    fun getMyRecordList(type: Int, userId: String): Flow<HttpResult<MutableList<MyRecordItemBean>>> {
        return HttpFamousService.service.getMyRecordList(type, userId)
    }

    fun getPublishBallList(): Flow<HttpResult<MutableList<PrizeBallBean>>> {
        return HttpFamousService.service.getPublishBallList()
    }

    fun joinChallenge(dareTypeId: Int, type: Int, successDesc: String, moneyId: Int, userId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("dareTypeId", dareTypeId)
        jsonObject.put("type", type)
        jsonObject.put("successDesc", successDesc)
        jsonObject.put("moneyId", moneyId)
        jsonObject.put("userId", userId)
        val body = jsonObject.toString().toRequestBody()
        return HttpFamousService.service.joinChallenge(body)
    }

    fun cancelChallenge(challengeRecordId: Int): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("id", challengeRecordId)
        val body = jsonObject.toString().toRequestBody()
        return HttpFamousService.service.cancelChallenge(body)
    }

    fun publishChallenge(
        content: String, contentDetail: String,
        dareTypeId: Int, type: Int,
        postType: Int, challengeRecordId: Int,
        remark: String, userId: String
    ): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("content", content)
        jsonObject.put("contentDetail", contentDetail)
        jsonObject.put("dareTypeId", dareTypeId)
        jsonObject.put("type", type)
        jsonObject.put("postType", postType)
        if (postType == 1) {
            jsonObject.put("challengeRecordId", challengeRecordId)
        }
        jsonObject.put("remark", remark)
        jsonObject.put("userId", userId)
        Logger.d(jsonObject.toString())
        val body = jsonObject.toString().toRequestBody()
        return HttpFamousService.service.publishChallenge(body)
    }

    fun setLike(dareContentId: Int, type: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("dareContentId", dareContentId)
        jsonObject.put("type", type)
        jsonObject.put("senderId", senderId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpFamousService.service.setLike(body)
    }

    fun setSubscribe(dareTypeId: Int, type: Int, subscriberId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("dareTypeId", dareTypeId)
        jsonObject.put("type", type)
        jsonObject.put("subscriberId", subscriberId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpFamousService.service.setSubscribe(body)
    }

    fun setReward(type: Int, number: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("type", type)
        jsonObject.put("number", number)
        jsonObject.put("senderId", senderId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpFamousService.service.setReward(body)
    }

    fun buyFlower(userId: String, number: Int): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("userId", userId)
        jsonObject.put("number", number)
        val body = jsonObject.toString().toRequestBody()
        return HttpFamousService.service.buyFlower(body)
    }

    fun setFollow(followUserId: String, masterUserId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("followUserId", followUserId)
        jsonObject.put("masterUserId", masterUserId)
        val body = jsonObject.toString().toRequestBody()
        return HttpFamousService.service.setFollow(body)
    }
}